//
//  ViewController.m
//  图片折叠
//
//  Created by 饶航 on 16/4/29.
//  Copyright © 2016年 Via. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV1;
@property (weak, nonatomic) IBOutlet UIImageView *imageV2;
@property (weak, nonatomic) CAGradientLayer *gradientL;
@property (weak, nonatomic) IBOutlet UIView *drawView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageV1.layer.contentsRect = CGRectMake(0, 0, 1, 0.5);
    self.imageV2.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5);
    
//    修改锚点
    self.imageV1.layer.anchorPoint = CGPointMake(0.5, 1);
    self.imageV2.layer.anchorPoint = CGPointMake(0.5, 0);
    
//    添加手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.drawView addGestureRecognizer:pan];
//    渐变层
    CAGradientLayer *gradientL = [CAGradientLayer layer];
    gradientL.frame = self.imageV2.bounds;
//    设置渐变颜色
    gradientL.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
    
//    设置图层的不透明度
    gradientL.opacity = 0;//不透明度
    self.gradientL = gradientL;
    [self.imageV2.layer addSublayer:gradientL];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView animateWithDuration:0.5 animations:^{
        self.imageV1.layer.transform = CATransform3DMakeRotation(M_PI, 1, 0, 0);

    }];
}

//- (void)gradientLayer{
////渐变层
//    CAGradientLayer *gradientL = [CAGradientLayer layer];
//    gradientL.frame = self.imageV2.bounds;
////    设置渐变颜色
//    gradientL.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor];
////    设置渐变的方向
//    gradientL.startPoint = CGPointMake(0, 0);
//    gradientL.endPoint = CGPointMake(1, 0);
//    
////    设置渐变的起始位置(从哪个位置起才开始有渐变) 设置阴影
//    gradientL.locations = @[@0.3,@0.7];
//
//    [self.imageV2.layer addSublayer:gradientL];
//
//}

- (void)pan:(UIPanGestureRecognizer *)pan{
//    让上部图片开始旋转
    
//    获取偏移量
    CGPoint TransP = [pan translationInView:pan.view];
    
//    计算透明度
    self.gradientL.opacity = TransP.y * 1 /200;
//    计算旋转的度数
    CGFloat angle = TransP.y * M_PI / 200.0;//200是图片的高度
//    添加透视效果(近大远小)
    CATransform3D transform = CATransform3DIdentity;
//    有透视效果(眼镜离屏幕的距离)
    transform.m34 = -1 / 300.0;
    
    self.imageV1.layer.transform = CATransform3DRotate(transform, -angle, 1, 0, 0);
    
//    当手指松开
    if (pan.state == UIGestureRecognizerStateEnded) {
//        Damping:弹性系数,数值越小,弹性越大,(0-1)
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.gradientL.opacity = 0;
//            复位
            self.imageV1.layer.transform = CATransform3DIdentity;
        } completion:nil];
    }

}
@end
